package wakawakathistimeforafrica.model;

import java.util.Enumeration;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jissay
 */
public class Client {
    
    private Instances dataSet;
    private Instance currentInstance;
    
    /**
     * 
     */
    public Client(Instances dataSet, 
                String checking_status,
                double duration,
                String credit_history,
                String purpose,
                double credit_amount,
                String savings_status,
                String employment,
                double installment_commitment,
                String personal_status,
                String other_parties,
                double residence_since,
                String property_magnitude,
                double age,
                String other_payment_plans,
                String housing,
                double existing_credits,
                String job,
                double num_dependents,
                String own_telephone,
                String foreign_worker) {
        
        this.dataSet = dataSet;
        
        Enumeration instances = this.dataSet.enumerateInstances();
        boolean instanceOk = false;
        
        while (instances.hasMoreElements() && !instanceOk) {
            Instance inst = (Instance) instances.nextElement();
            if (inst != null) {
                if (inst.numAttributes() == 21) {
                    instanceOk = true;
                    this.currentInstance = inst;
                    this.currentInstance.setMissing(20);
                }
            }
        }
        
        String[] attrNames = {"checking_status", "duration", "credit_history", "purpose", "credit_amount", "savings_status", "employment", "installment_commitment", "personal_status", "other_parties", "residence_since", "property_magnitude", "age", "other_payment_plans", "housing", "existing_credits", "job", "num_dependents", "own_telephone", "foreign_worker"};
        Object[] attrValues = {checking_status, duration, credit_history, purpose, credit_amount, savings_status, employment, installment_commitment, personal_status, other_parties, residence_since, property_magnitude, age, other_payment_plans, housing, existing_credits, job, num_dependents, own_telephone, foreign_worker};
        if (this.currentInstance.numAttributes() == 21) {
            Enumeration instanceAttributes = this.currentInstance.enumerateAttributes();
            
            while (instanceAttributes.hasMoreElements()) {
                Attribute attr = (Attribute) instanceAttributes.nextElement();
                for (int i = 0; i < 20; i++) {
                    if (attr.name().equals(attrNames[i])) {
                        
                        // Get values and check if exists
                        Enumeration values = attr.enumerateValues();
                        boolean containValue = false;
                        while (values != null && values.hasMoreElements()) {
                            Object val = values.nextElement();
                            
                            if (val.equals(attrValues[i])) {
                                containValue = true;
                                break;
                            }
                        }
                        
                        double doubleVal = 0.0f;
                        String stringVal = "";
                        boolean isDouble = false;
                        boolean isString = false;
                        
                        
                        if (attrValues[i].getClass().equals(String.class)) {
                            stringVal = attrValues[i].toString();
                            isString = true;
                        } else if (attrValues[i].getClass().equals(double.class)) {
                            doubleVal = Double.parseDouble(attrValues[i].toString());
                            isDouble = true;
                        }
                        
                        // Add the value if not exist
                        if (!containValue && isString) {
                            attr.addStringValue(attrValues[i].toString());
                        }
                        
                        // Set the value to the current instance
                        if (isString) {
                            if (attr.indexOfValue(stringVal) == -1) {
                                attr.addStringValue(stringVal);
                            }
                            this.currentInstance.setValue(attr, stringVal);
                        } else if (isDouble) {
                            this.currentInstance.setValue(attr, doubleVal);
                        }
                        
                        i=20;
                        
                    }
                }
            }
        }
        
//        
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), duration);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), credit_history);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), purpose);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), credit_amount);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), savings_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), employment);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), installment_commitment);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), personal_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), other_parties);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        this.currentInstance.setValue((Attribute)instanceAttributes.nextElement(), checking_status);
//        
//        this.setValue(new Attribute("duration"), duration);
//        this.setValue(new Attribute("credit_history"), credit_history);
//        this.setValue(new Attribute("purpose"), purpose);
//        this.setValue(new Attribute("credit_amount"), credit_amount);
//        this.setValue(new Attribute("savings_status"), savings_status);
//        this.setValue(new Attribute("employment"), employment);
//        this.setValue(new Attribute("installment_commitment"), installment_commitment);
//        this.setValue(new Attribute("personal_status"), personal_status);
//        this.setValue(new Attribute("other_parties"), other_parties);
//        this.setValue(new Attribute("residence_since"), residence_since);
//        this.setValue(new Attribute("property_magnitude"), property_magnitude);
//        this.setValue(new Attribute("age"), age);
//        this.setValue(new Attribute("other_payment_plans"), other_payment_plans);
//        this.setValue(new Attribute("housing"), housing);
//        this.setValue(new Attribute("existing_credits"), existing_credits);
//        this.setValue(new Attribute("job"), job);
//        this.setValue(new Attribute("num_dependents"), num_dependents);
//        this.setValue(new Attribute("own_telephone"), own_telephone);
//        this.setValue(new Attribute("foreign_worker"), foreign_worker);
//        this.setValue(new Attribute("class"), theclass);
        
    }

    public Instances getDataSet() {
        return dataSet;
    }

    public void setDataSet(Instances dataSet) {
        this.dataSet = dataSet;
    }

    public Instance getCurrentInstance() {
        return currentInstance;
    }

    public void setCurrentInstance(Instance currentInstance) {
        this.currentInstance = currentInstance;
    }
}
